package Netz;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Dies ist die Spieloberflaechte wo die Battles stattfinden.
 * @author Daniel Mueller, Phillip Hertel, Jonas Hauff
 */
public class Netze extends javax.swing.JFrame {
    private NetzeClient client;
    private Lobbys lobby; 

    /**
     * Creates new form Netze
     */
    public Netze() {
        initComponents();
        Feld00.setEnabled(true);
        Feld01.setEnabled(true);
        Feld02.setEnabled(true);
        Feld10.setEnabled(true);
        Feld11.setEnabled(true);
        Feld12.setEnabled(true);
        Feld20.setEnabled(true);
        Feld21.setEnabled(true);
        Feld22.setEnabled(true);
    }
    /**
     * Das Label fuer den Namen des aktuellen Spielers wird auf den Namen im Parameter gesetzt.
     * @param pName 
     */
    public void setpName(String pName)
    {
        jLabelName.setText(pName);
    }
    /**
     * Der Client wird festgelegt.
     * @param pClient 
     */
    public void setClient(NetzeClient pClient)
    {
        client = pClient;
        jLabelName.setText(client.getName());
        
    }
    /**
     * Der Client wird ausgegeben.
     * @return 
     */
    public NetzeClient getClient()
    {
        return client;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        Feld00 = new javax.swing.JButton();
        Feld01 = new javax.swing.JButton();
        Feld02 = new javax.swing.JButton();
        Feld10 = new javax.swing.JButton();
        Feld11 = new javax.swing.JButton();
        Feld12 = new javax.swing.JButton();
        Feld20 = new javax.swing.JButton();
        Feld21 = new javax.swing.JButton();
        Feld22 = new javax.swing.JButton();
        Beenden = new javax.swing.JButton();
        Spieler = new javax.swing.JLabel();
        jLabelAngemeldetAls = new javax.swing.JLabel();
        jLabelName = new javax.swing.JLabel();
        jLabelAktGegner = new javax.swing.JLabel();
        jLabelGegnerName = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        Feld00.setEnabled(false);
        Feld00.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Feld00ActionPerformed(evt);
            }
        });

        Feld01.setEnabled(false);
        Feld01.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Feld01ActionPerformed(evt);
            }
        });

        Feld02.setEnabled(false);
        Feld02.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Feld02ActionPerformed(evt);
            }
        });

        Feld10.setEnabled(false);
        Feld10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Feld10ActionPerformed(evt);
            }
        });

        Feld11.setEnabled(false);
        Feld11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Feld11ActionPerformed(evt);
            }
        });

        Feld12.setEnabled(false);
        Feld12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Feld12ActionPerformed(evt);
            }
        });

        Feld20.setEnabled(false);
        Feld20.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Feld20ActionPerformed(evt);
            }
        });

        Feld21.setEnabled(false);
        Feld21.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Feld21ActionPerformed(evt);
            }
        });

        Feld22.setEnabled(false);
        Feld22.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Feld22ActionPerformed(evt);
            }
        });

        Beenden.setText("Beenden");
        Beenden.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BeendenActionPerformed(evt);
            }
        });

        jLabelAngemeldetAls.setText("Angemeldet als:");

        jLabelName.setText("Name");

        jLabelAktGegner.setText("aktueller Gegner:");

        jLabel1.setText("X:");

        jLabel2.setText("O:");

        jLabel3.setText("Zeichen");

        jLabel4.setText("Du");

        jLabel5.setText("Gegner");

        jLabel6.setText("TicTacToe");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(120, 120, 120)
                .addComponent(jLabel6)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(66, 66, 66)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Beenden, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(Feld20, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(Feld21, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(Feld22, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(Feld10, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(Feld00, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(Feld11, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(Feld12, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(Feld01, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(Feld02, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabelAngemeldetAls)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jLabelName))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabelAktGegner)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jLabelGegnerName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel5))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel4)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Spieler)))
                        .addContainerGap(252, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addComponent(jLabel6)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelAngemeldetAls)
                    .addComponent(jLabelName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(Feld01, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(Feld02, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(Feld00, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(Feld10, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(Feld11, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(Feld12, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(Feld20, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Feld21, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Feld22, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelAktGegner)
                    .addComponent(jLabelGegnerName, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Spieler)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Beenden)
                .addGap(0, 84, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

            
    /**
     * Es wird an den Server gesendet, dass der Spieler ddieses Feld makieren will.
     * @param evt 
     */    
    private void Feld01ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Feld01ActionPerformed
        if(Feld01.getText().equals(""))
        {
            client.pSend("Feld makieren: Feld2");
        }
    }//GEN-LAST:event_Feld01ActionPerformed
    /**
     * Es wird an den Server gesendet, dass der Spieler ddieses Feld makieren will.
     * @param evt 
     */
    private void Feld02ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Feld02ActionPerformed
        if(Feld02.getText().equals(""))
        {
            client.pSend("Feld makieren: Feld3");
        }
    }//GEN-LAST:event_Feld02ActionPerformed
    /**
     * Es wird an den Server gesendet, dass der Spieler ddieses Feld makieren will.
     * @param evt 
     */
    private void Feld10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Feld10ActionPerformed
      if(Feld10.getText().equals(""))
        {
            client.pSend("Feld makieren: Feld4");
        }
    }//GEN-LAST:event_Feld10ActionPerformed
    /**
     * Es wird an den Server gesendet, dass der Spieler ddieses Feld makieren will.
     * @param evt 
     */
    private void Feld11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Feld11ActionPerformed
        if(Feld11.getText().equals(""))
        {
            client.pSend("Feld makieren: Feld5");
        }
    }//GEN-LAST:event_Feld11ActionPerformed
    /**
     * Es wird an den Server gesendet, dass der Spieler ddieses Feld makieren will.
     * @param evt 
     */
    private void Feld12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Feld12ActionPerformed
        if(Feld12.getText().equals(""))
        {
            client.pSend("Feld makieren: Feld6");
        }
    }//GEN-LAST:event_Feld12ActionPerformed
    /**
     * Es wird an den Server gesendet, dass der Spieler ddieses Feld makieren will.
     * @param evt 
     */
    private void Feld20ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Feld20ActionPerformed
        if(Feld20.getText().equals(""))
        {
            client.pSend("Feld makieren: Feld7");
        }
    }//GEN-LAST:event_Feld20ActionPerformed
    /**
     * Es wird an den Server gesendet, dass der Spieler ddieses Feld makieren will.
     * @param evt 
     */
    private void Feld21ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Feld21ActionPerformed
        if(Feld21.getText().equals(""))
        {
            client.pSend("Feld makieren: Feld8");
        }
    }//GEN-LAST:event_Feld21ActionPerformed

    private void Feld22ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Feld22ActionPerformed
        if(Feld22.getText().equals(""))
        {
            client.pSend("Feld makieren: Feld9");
        }
    }//GEN-LAST:event_Feld22ActionPerformed
    /**
     * Es wird an den Server gesendet, dass der Spieler ddieses Feld makieren will.
     * @param evt 
     */
    private void Feld00ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Feld00ActionPerformed
        if(Feld00.getText().equals(""))
        {
            client.pSend("Feld makieren: Feld1");
        }
    }//GEN-LAST:event_Feld00ActionPerformed
    /**
     * Verstellt die Felder, je nachdem ob der Spieler nun an der Reihe ist oder nicht.
     * @param p 
     */
    public void setAnderReihe(boolean p)
    {
        if(p==true)
        {
            Feld00.setEnabled(true);
            Feld01.setEnabled(true);
            Feld02.setEnabled(true);
            Feld10.setEnabled(true);
            Feld11.setEnabled(true);
            Feld12.setEnabled(true);
            Feld20.setEnabled(true);
            Feld21.setEnabled(true);
            Feld22.setEnabled(true);
        }
        if(p==false)
        {
            Feld00.setEnabled(false);
            Feld01.setEnabled(false);
            Feld02.setEnabled(false);
            Feld10.setEnabled(false);
            Feld11.setEnabled(false);
            Feld12.setEnabled(false);
            Feld20.setEnabled(false);
            Feld21.setEnabled(false);
            Feld22.setEnabled(false);
        }
    }
    /**
     * Das Feld das im Parameter uebergeben wird, wird mit dem Symbol des aktuellen Spielers makiert.
     * @param pFeld 
     */
    public void feldMakieren(String pFeld)
    {
        switch(pFeld)
        {
            case "Feld1":
            {
                Feld00.setText("X");
                break;
            }
            case "Feld2":
            {
                Feld01.setText("X");
                break;
            }
            case "Feld3":
            {
                Feld02.setText("X");
                break;
            }
            case "Feld4":
            {
                Feld10.setText("X");
                break;
            }
            case "Feld5":
            {
                Feld11.setText("X");
                break;
            }
            case "Feld6":
            {
                Feld12.setText("X");
                break;
            }
            case "Feld7":
            {
                Feld20.setText("X");
                break;
            }
            case "Feld8":
            {
                Feld21.setText("X");
                break;
            }
            case "Feld9":
            {
                Feld22.setText("X");
                break;
            }
        }
    }
    /**
     * Das ueberreichte Feld wird mit demn Symbol des Gegners makiert.
     * @param pFeld 
     */
    public void gegnerFeldMakieren(String pFeld)
    {
        switch(pFeld)
        {
            case "Feld1":
            {
                Feld00.setText("O");
                break;
            }
            case "Feld2":
            {
                Feld01.setText("O");
                break;
            }
            case "Feld3":
            {
                Feld02.setText("O");
                break;
            }
            case "Feld4":
            {
                Feld10.setText("O");
                break;
            }
            case "Feld5":
            {
                Feld11.setText("O");
                break;
            }
            case "Feld6":
            {
                Feld12.setText("O");
                break;
            }
            case "Feld7":
            {
                Feld20.setText("O");
                break;
            }
            case "Feld8":
            {
                Feld21.setText("O");
                break;
            }
            case "Feld9":
            {
                Feld22.setText("O");
                break;
            }
        }
    }
    /**
     * Das Spiel wird beendet und der Spieler kommt in die Lobby.
     * @param evt 
     */
    private void BeendenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BeendenActionPerformed
        client.send("Spiel beenden");
        this.setVisible(false);
        lobby.setVisible(true);
        
    }//GEN-LAST:event_BeendenActionPerformed
    /**
     * Die Lobby wird uebergeben.
     * @param pLobby 
     */
    public void setLobby(Lobbys pLobby)
    {
        lobby = pLobby;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Netze.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Netze.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Netze.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Netze.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Netze().setVisible(true);
                
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Beenden;
    private javax.swing.JButton Feld00;
    private javax.swing.JButton Feld01;
    private javax.swing.JButton Feld02;
    private javax.swing.JButton Feld10;
    private javax.swing.JButton Feld11;
    private javax.swing.JButton Feld12;
    private javax.swing.JButton Feld20;
    private javax.swing.JButton Feld21;
    private javax.swing.JButton Feld22;
    private javax.swing.JLabel Spieler;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabelAktGegner;
    private javax.swing.JLabel jLabelAngemeldetAls;
    private javax.swing.JLabel jLabelGegnerName;
    private javax.swing.JLabel jLabelName;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables
}
